<!--
  - ControllerCarbohydrate.php
  - v1.3
  - 4/02/2011
  -->

<!--
  - Code Specifications:
  - This code processes the inputs of the user in ViewCarbohydrate.php
  - This also stores the inputs, mean, and standard deviation into the 
  - database table.
  -->
	
<?php
	//Gets required libraries
	require '../libraries/EditAnalysisLib.php';
	require '../libraries/ComputeAnalysisLib.php';
	require '../libraries/UserLibrary.php';
	
	//Formally starts session
	session_start();
	
	//Ensures that the analyst is logged in.
	if(isSessionSet("analyst", NULL, "../Logout.php"));
	
	//Gathers data input from ViewCarbohydrate.php
	$percentMoisture1 =  $_POST['17/1'];
	$percentProtein1 = $_POST['18/1'];
	$percentFat1 = $_POST['19/1'];	
	$percentAsh1 = $_POST['20/1'];
	$percentFiber1 = $_POST['21/1'];
	$percentMoisture2 =  $_POST['17/2'];
	$percentProtein2 = $_POST['18/2'];
	$percentFat2 = $_POST['19/2'];	
	$percentAsh2 = $_POST['20/2'];
	$percentFiber2 = $_POST['21/2'];
	
	//Create connection to the database
	$connect = mysql_connect("localhost","editanalysis","editanalysis");	
	if (!$connect) {
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("casl", $connect);
	
	//Execute query
	$val = mysql_query("SELECT * FROM analysis_result WHERE casl_year = 
			$_SESSION[year] AND casl_sample = $_SESSION[no]");
	$row = mysql_fetch_array($val);
		
	//Computes the carbohydrate content from the first column of inputs
	if (isset($_POST['compute1'])) {
		$compute1 = ComputeCarbohydrate($percentMoisture1, $percentProtein1, 
								$percentFat1, $percentAsh1, $percentFiber1);
		$mean = ComputeMean($compute1, $row['result_value2'], 0, 2);
		$standardDeviation = ComputeStandardDeviation($compute1, 
				$row['result_value2'], $mean, 2);
		$edit = editCarbohydrate($standardDeviation, $mean, "result_value1", 
				$compute1, "value1", $percentMoisture1, $percentProtein1, 
				$percentFat1, percentAsh1, $percentFiber1, $_SESSION["year"],
				$_SESSION["no"], 17, 18, 19, 20, 21, 
				"casl", "localhost", "editanalysis", "editanalysis");
	//Computes the ash content from the second column of inputs
	} else if (isset($_POST['compute2'])) {
		$compute2 = ComputeCarbohydrate($percentMoisture2, $percentProtein2, 
				$percentFat2, $percentAsh2, $percentFiber2);	
		$mean = ComputeMean($compute2, $row['result_value1'], 0, 2);
		$standardDeviation = ComputeStandardDeviation($compute2,
				$row['result_value1'], $mean, 2);
		$edit = editCarbohydrate($standardDeviation, $mean, "result_value2",
				$compute2, "value2", $percentMoisture2, $percentProtein2, 
				$percentFat2, $percentAsh2, $percentFiber2, 
				$_SESSION["year"], $_SESSION["no"], 17, 18, 19, 20, 21, 
				"casl", "localhost", "editanalysis", "editanalysis");
	//Computes the ash content from both columns of inputs
	} else if (isset($_POST['computeBoth'])) {
		$compute1 = ComputeCarbohydrate($percentMoisture1, $percentProtein1, 
				$percentFat1, $percentAsh1, $percentFiber1);
		$compute2 = ComputeCarbohydrate($percentMoisture2, $percentProtein2, 
				$percentFat2, $percentAsh2, $percentFiber2);	
		$mean = ComputeMean($compute1, $compute2, 0, 2);
		$standardDeviation = ComputeStandardDeviation($compute1, $compute2,
				$mean, 2);
		$edit1 = editCarbohydrate($standardDeviation, $mean, "result_value1",
				$compute1, "value1", $percentMoisture1, $percentProtein1, 
				$percentFat1, $percentAsh1, $percentFiber1, $_SESSION["year"],
				$_SESSION["no"], 17, 18, 19, 20, 21, "casl", "localhost", 
				"editanalysis", "editanalysis");
		$edit2 = editCarbohydrate($standardDeviation, $mean, "result_value2",
				$compute2, "value2", $percentMoisture2, $percentProtein2, 
				$percentFat2, $percentAsh2, $percentFiber2, $_SESSION["year"], 
				$_SESSION["no"], 17, 18, 19, 20, 21, "casl", "localhost", 
				"editanalysis", "editanalysis");
	}
	
	//Redirect back to ViewCarbohydrate.php
	header("Location: ViewCarbohydrate.php");
?>